//获取目标元素
var myself=document.getElementById('mySelf');
//获取body
var body=document.body;
//获取html或者body的宽高
var w=document.documentElement.offsetWidth || body.offsetWidth;
var h=window.innerHeight || document.documentElement.offsetHeight || body.offsetHeight;

	//速度
	var speed=1;
document.onkeydown=function  (e) {
	var e=e ||window.event; 
//console.log(e.keyCode)  39右37左40下38上
	

	/*左 上 右 下*/
	if (e.keyCode==37) {
		clearInterval(myself.timer);
		myself.timer=setInterval(function(){
					
			if (myself.offsetLeft<=0) {
				myself.style.left=0+'px';
			} else{
				myself.style.left=myself.offsetLeft - speed +"px";
			}
		},1)

		
	}else if (e.keyCode==38) {	
		clearInterval(myself.timer);
		myself.timer = setInterval(function(){
			if (myself.offsetTop<=0) {
				myself.style.top=0 +'px';
			} else{
				myself.style.top=myself.offsetTop - speed +"px";
			}
		},1)

		
	}else if(e.keyCode==39){
		clearInterval(myself.timer);
		myself.timer=setInterval(function(){
			if (myself.offsetLeft >= w - myself.offsetWidth-20) {
				myself.style.left=w - myself.offsetWidth+speed-20 +'px'
			} else{
				myself.style.left=myself.offsetLeft + speed +"px";
			}			
		},1)

	}else if(e.keyCode==40){
		clearInterval(myself.timer);
			myself.timer=setInterval(function(){
			if (myself.offsetTop >= h - myself.offsetHeight-20) {
				myself.style.top=h - myself.offsetHeight-20+speed +'px'
			} else{
				myself.style.top=myself.offsetTop + speed +"px";
			}		
		})
	
	}else if(e.keyCode==32){
		
		clearInterval(myself.timer1)
		
		myself.timer1=setInterval(function  () {
			
		var bullet=document.createElement('div');
		var bullet1=document.createElement('div');
		//给子弹添加类名
		bullet.setAttribute('class','b');
		bullet1.setAttribute('class','b');
		//把子弹添加到body中
		body.appendChild(bullet);
		body.appendChild(bullet1);
		
		//子弹的位置
		
		bullet.style.left=myself.offsetLeft+myself.offsetWidth/4+"px";
		bullet1.style.left=myself.offsetLeft+myself.offsetWidth*(3/4)+"px";
		
		bullet.style.top=myself.offsetTop+'px';
		
		bullet1.style.top=myself.offsetTop+'px';
			
		},500)
		//创建子弹
		
		
		
	}
	
	
	
}

//让子弹飞
function fly () {
	
	var ob=document.getElementsByClassName('b');
	
	for(var i=0; i<ob.length;i++){
		
		ob[i].style.top=ob[i].offsetTop- speed +"px";
		if(ob[i].ofafsetTop< 10){
			body.removeChild(ob[i])
		}
	}
	
}

var num=0;
var xue=0;
setInterval(function(){
	fly();
	num++;
	if (num%200==0) {
			Dplan();
	}

	djfly();
	rE();
},5);


Dplan()
//敌机、
function Dplan(){
	
	var ran=Math.random() //0-1
	
	if(ran>0.9){
		var  lj=document.createElement('span');
		var y=Math.ceil(Math.random()*(w-150 -100)+100)
		lj.style.left= y + (speed/4) + 'px';
		lj.setAttribute('class','span2');  //lj.className=
		 lj.xue=8;
		body.appendChild(lj);
	}else{
		var  dj=document.createElement('span');	
		var x=Math.ceil(Math.random()*(w-150 -100)+100)
		dj.style.left= x + (speed/2) + 'px';
		dj.setAttribute('class','span1');
		dj.xue=4;
		body.appendChild(dj);
	}
	
	
	
}

//让敌机飞

function  djfly(){
	
	var dj=document.querySelectorAll('.span1');
	var lj=document.querySelectorAll('.span2');
	
//	for(var i=0; i<dj.length;){
//		
//	}
	
		dj.forEach(function  (p1) {
			p1.style.top=p1.offsetTop+ speed/2 + 'px';
			if (p1.offsetTop>h-p1.offsetHeight-10) {
				body.removeChild(p1)
			}	
		})
		lj.forEach(function  (p1) {
			p1.style.top=p1.offsetTop+ speed + 'px';
			if (p1.offsetTop>h-p1.offsetHeight-10) {
				body.removeChild(p1)
			}	
		})
}



//碰撞
function zuang(b,d){
	
	//飞机
	var bl=b.offsetLeft;
	var bt=b.offsetTop;
	var bw=b.offsetWidth;
	var bh=b.offsetHeight;
	
	//敌机
	var dl=d.offsetLeft;
	var dt=d.offsetTop;
	var dw=d.offsetWidth;
	var dh=d.offsetHeight;
	
	if(dl<bl+bw && dt<bt+bh && bl<dl+dw && bt<dt+dh){
		return true;
	}else{
		return false;
	}
	
}


//移除相撞的飞机和子弹
function rE(){
	
	var bullets=document.querySelectorAll('.b')//获取子弹
	
	var dj=document.querySelectorAll('span');//敌机
	
	
	var txt=document.getElementsByTagName('b')[0];
	
	for(var i=0;i<dj.length;i++){
		
		for(var j=0;j<bullets.length;j++){
			
			if(zuang(bullets[j],dj[i])){
				
//				if (dj[i].getAttribute('class')) {
//					
//					if( dj[i].className.indexOf('span2')>-1){
//					
//						xue--;			
//						if(xue==0){	
//								alert(1)
//							body.removeChild(bullets[j]);
//							//txt=txt+100;
//						}
//						
//					}else{
//						//alert(2)
//						xue--;
//						if(xue==0){	
//							alert(2)
//							body.removeChild(bullets[j]);
//							//txt=txt+50;
//						}
//						
//					}
//				}
		
	
					dj[i].xue=dj[i].xue-1;
				
						if (dj[i].xue == 0) {
							//alert(12);
							
							if (dj[i].getAttribute('class')) {
								
								if (dj[i].className.indexOf('span2')>-1) {
									//alert('22')
								
									txt.innerHTML=Number(txt.innerText)+100
								} else{
									//alert(1)
									txt.innerHTML=Number(txt.innerText)+50
								}
								
							}
							
							
							body.removeChild(dj[i])
						}
				
					body.removeChild(bullets[j])
				
			}
			
		}
		
	}
	
	
	
}
